﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MovingChame.Web.Models;

namespace MovingChame.Web.Controllers
{
    public class TourController : Controller
    {
        private MovingChameModelContainer db = new MovingChameModelContainer();

        //
        // GET: /Tour/

        public ActionResult Index()
        {
            var abstracttours = db.Tours.Include(a => a.User);
            return View(abstracttours.ToList());
        }

        //
        // GET: /Tour/Details/5

        public ActionResult Details(int id = 0)
        {
            Tour tour = db.Tours.Find(id);
            if (tour == null)
            {
                return HttpNotFound();
            }
            return View(tour);
        }

        //
        // GET: /Tour/Create

        public ActionResult Create()
        {
            ViewBag.User_Id = new SelectList(db.Users, "Id", "Username");
            return View();
        }

        //
        // POST: /Tour/Create

        [HttpPost]
        public ActionResult Create(Tour tour)
        {
            if (ModelState.IsValid)
            {
                db.Tours.Add(tour);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.User_Id = new SelectList(db.Users, "Id", "Username", tour.UserId);
            return View(tour);
        }

        //
        // GET: /Tour/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Tour tour = db.Tours.Find(id);
            if (tour == null)
            {
                return HttpNotFound();
            }
            ViewBag.User_Id = new SelectList(db.Users, "Id", "Username", tour.UserId);
            return View(tour);
        }

        //
        // POST: /Tour/Edit/5

        [HttpPost]
        public ActionResult Edit(Tour tour)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tour).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.User_Id = new SelectList(db.Users, "Id", "Username", tour.UserId);
            return View(tour);
        }

        //
        // GET: /Tour/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Tour tour = db.Tours.Find(id);
            if (tour == null)
            {
                return HttpNotFound();
            }
            return View(tour);
        }

        //
        // POST: /Tour/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Tour abstracttour = db.Tours.Find(id);
            db.Tours.Remove(abstracttour);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}